  set more off
  set seed 19112014

*===================================================================*
*   BIHAR EVALUATION OF SOCIAL FRANCHISING AND TELEMEDICINE (BEST)
*                   Processing SP data - Baseline
*====================================================================*


*====================================================================*
* DIARRHEA
*====================================================================*

* Open database
  use "$prodata1\standardized_patient_diarrhea", clear

* Sample Selection II
/*exclude doctor that only asked the father to bring the child
  c1 did doctor ask to bring the child
  c2 treatment not given(1)/given(2) */
  egen qmiss=rowmean(h1a-h16b)
  replace sample2 = 3 if c1==1 & c2==1 & qmiss==2
  replace sample2 = 4 if c1==2 & c2==1 & qmiss==2
  replace sample2 = 5 if prov_id==16102 // not interviewed
  lab def sample2 1 "complete" 2 "incorrect statement" 3 "child + nothing" 4 "neither child nor treatment" 5 "not interviewed"
  lab val sample2 sample2
  drop qmiss


***********************************************
* General Questions
***********************************************

* Source
  gen     source = "SP Baseline"
  lab var source "Data source"

* Wave
  gen     wave = 1
  lab var wave "Wave"

* Date SP interaction
  replace qq10="25.09.2011" if qq10=="25.29.2011"
  gen     sp_date=date(qq10,"DMY")
  format  sp_date %d
  lab var sp_date "Date of SP-Provider Interaction"

* SP id
  gen     spp_id = qq11a 
  lab var spp_id "Standarized Patient id"
  
* Interviewer id
  gen     intv_id = qq12a
  lab var intv_id "Interviewer id"

* Day of week for visit
  gen day =day(sp_date)
  gen month=month(sp_date)
  gen year=year(sp_date)
  gen dow = dow(mdy(month, day,year))
  lab var dow "Day of week of SP interaction"
  lab def week 0 "Sunday" 1 "Monday" 2 "Tuesday" 3 "Wednesday" 4 "Thursday" 5 "Friday" 6 "Saturday"
  lab val dow week
  drop day month year

* Hour of visit
  gen qq8n=qq8
  replace qq8n ="14:46" if qq8=="02:46"
  replace qq8n ="15:54" if qq8=="03:54"
  gen double ctime =clock(qq8n, "hm")
  format ctime %tcHH:MM
  gen hour=hh(ctime)
  gen minute = mm(ctime)
  lab var ctime  "Consultation Time"
  lab var hour   "Hour of SP interaction"
  lab var minute "Minutes of SP interaction"
  drop qq8n

* Visit Length (minutes)
  split qq9, p(:)
  destring qq91 qq92 qq93, replace
  gen     visit_time = qq91*60 + qq92 + qq93/60
  format  visit_time %9.2f
  lab var visit_time "visit length (minutes)"
  gen double vtime =clock(qq9, "hms")
  format vtime %tcMM:SS
  cap drop qq9* 

* Patient Caseload
  gen     pat_before=n1
  lab var pat_before "No. of patient before visit"
  gen     pat_after=n2
  lab var pat_after "No. of patient after visit"
  egen    caseload=rsum(pat_before pat_after)
  lab var caseload "patient caseload"

* Doctor ask to bring the child
/*c1: Did doctor ask to bring the child?*/
  recode c1 (2=0), gen (ask_child)
  lab var ask_child "=1 if doctor asked to bring the child"


***********************************************
* Medical History
***********************************************
/*For diarrhea case (h?a)
  H1: Age of child
  H2: Qualities of stool (including blood)
  H3: Frequency
  H4: Quantity of stool
  H5: Questions about urination
  H6: Is child active/ playful? General behavior of child.
  H7: Fever
  H8: Abdominal pain
  H9: Vomiting?
  H10: Previous health status
  H11: Source of drinking water? Boiling water? From tap? How is it stored?
  H12: Food preparation
  H13: What has the child eaten?
  H14: Taking fluids */

* Recoding variables
  foreach v of varlist h1a-h16b {
    replace `v'=0 if `v'==2
      }

* Recoding values according to extra questions
  replace h2a=1 if prov_id==38902
  replace h3a=1 if prov_id==31104
  replace h8a=1 if prov_id==13305
  replace h8a=1 if prov_id==19305
  replace h8a=1 if prov_id==20703
  replace h8a=1 if prov_id==22505
  replace h8a=1 if prov_id==27305
  replace h8a=1 if prov_id==27905
  replace h8a=1 if prov_id==35603
  replace h8a=1 if prov_id==39801
  replace h10a=1 if prov_id==20401
  replace h13a=1 if prov_id==27305
  replace h13a=1 if prov_id==38902
  replace h14a=1 if prov_id==5405
  replace h14a=1 if prov_id==30304

* Reorder variables
  order h1b, after(h16a)
  forvalues n=2/16 {
  local nn=`n'-1
  order h`n'b, after(h`nn'b)
    }

* Number of Medical History questions asked (including social history)
  egen    nhistory = rowtotal(h1a-h16a), missing
  lab var nhistory "No. of Medical History questions asked"

* Essential question for cause of desease
/*h2a qualities of stool
  h7a fever
  h8a abdominal pain
  h9a vomiting
  h12 food preparation
  h13 what has eaten */
  egen    qcause=rsum(h2a h7a-h9a h12a h13a)
  lab var qcause "No. of ques. asked related to cause of disease"

* Essential question for severity of desease
/*h3a frequency
  h4a quantity of stool
  h5a urination
  h6a active/playful
  h14 taking fluids*/
  egen    qseverity = rsum(h3a-h6a h14a)
  lab var qseverity "No. of ques. asked related to severity of disease"

* Number of essential items obtained by providers in history-taking
  egen    nessential=rsum(qcause qseverity)
  lab var nessential "No. of essential items obtained in history-taking"

* Percentage of essential items obtained by providers in history-taking
  gen     pessential = nessential
  replace pessential = pessential/11
  lab var pessential "% of essential items obtained in history-taking"

***********************************************
* Social History
***********************************************
/*For Diarrhea case
  H15: Any question about neighborhood/family background
  H16: Physical Environment */

* Social history taking
  egen    qsocial=rsum(h15a h16a)
  replace qsocial=1 if qsocial>1 & qsocial!=.
  lab var qsocial "=1 if provider asked about social history"


***********************************************
* Treatment
***********************************************

* Any Treatment
  recode c2 (1=0) (2=1), gen(tx_any)
  egen aux=rownonmiss(t1_?med t2_?med), strok
  replace tx_any=1 if (aux>0 & aux!=.)
  lab var tx_any "=1 if provider gave treatment"
  drop aux

* Recode Variables
  forvalues n=1/3 {
  recode t`n' (2=0)
    }

* Medicines dispensed/prescribed
/*t1: Medicines dispensed; t2: Medicines prescribed*/
  gen     med_dispensed = t1
  lab var med_dispensed "medicines dispensed"
  gen     med_prescribed = t2
  lab var med_prescribed "medicines prescribed"


***********************************************
* Correct Treatment
***********************************************

* Merge with correct treatment
  sort case prov_id
  merge 1:1 case prov_id using "$rawdatap1\correct_treatment_sp.dta"
  keep if _merge==3
  drop _merge tx_detail3 nantibiotics_c3

* Rename
  lab var tx_detail1 "detailed treatment"
  lab var nmedicines "# of medicines prescribed"
  rename nantibiotics_c1 nantibiotics1
  lab var nantibiotics1 "# of antibiotics"

***********************************************
* Recomendations
***********************************************

* Counselling on hygiene especially washing hands
/*t3: Counseing on hygiene especially washing hands*/
  gen     counselling = t3
  lab var counselling "=1 if counselling on hygiene"

* Explanation about the medicines.
/*t4: Explanation about the medicines: 1=Self Explained; 2=SP asked; 3=No one*/
  gen     explanation = t4
  lab var explanation "=1 if provides explanation about medicines"
  lab def exp 1 "Self-explained" 2 "SP asked" 3 "No one"
  lab val explanation exp

* Explanation given by whom?
/*t5: Explanation given by whom? 1=Doctor; 2=Compounder; 3=Nurse*/
  gen     expl_who = t5
  lab var expl_who "who gave the explanation"
  lab def expl 1 "doctor" 2 "Compounder" 3 "nurse"
  lab val expl_who expl 

* Number of questions if treatment/no treatment
  gen     qs_treat = nhistory if tx_any==1
  lab var qs_treat "number of questions asked when treatment given"

  gen     qs_notreat = nhistory if tx_any==0
  lab var qs_notreat "number of questions asked when no treatment given"


***********************************************
* Diagnosis
***********************************************
/*D1: Did the doctor discuss a possible diagnosis?
  D2: If yes what was the diagnosis? (one or more than one)
  D3: Was the diagnosis correct?*/

* Recode
  foreach v of varlist d1 d3 {
  recode `v' (2=0)
    }

* Any Diagnosis
  gen     dx_any = d1
  lab var dx_any "=1 if any kind of diagnosis was given"

* Diagnosis
  gen     dx = d2
  lab var dx "Diagnosis"

* Type of dignosis given
  gen code=soundex(d2)
  gen     dx_type = 0
  replace dx_type = 1 if code=="D600"
  lab var dx_type "=1 if diagnosis was corrrect, when done"
  drop code

***********************************************
* Fees
***********************************************
/*F1: Fee charged by practitioner
  F2: Cost of medicines paid in addition
  F3: Total Fee */

* Fee
  gen     fee = f1
  lab var fee "fee charged by practitioner"
  gen     fee_med =f2
  lab var fee_med "cost of medicines paid in addition"
  gen     fee_total=f3
  lab var fee_total "total cost"

***********************************************
* Follow up questions
***********************************************
/*R1:Did the doctor ask you to return? if yes ,then mark the following options If no=2 go to next section
  R2:If you do not feel better. 
  R3: To get a refill of the medicine 
  R4: After completion of recommended test 
  R5a: After so many days
  r5b: Weeks */
  gen     ask_return=(r1==1)
  lab var ask_return "=1 if doctor ask the patient to return"

* When did he/she asked you to return
  forvalues n=1/3 {
  local m=`n'+1
  gen return_`n'=(r`m'==1) if ask_return==1
    }
  gen     return_4 =(r5a!=.|r5b!=.) if ask_return==1
  replace return_4 = 0 if r5a==0 & r5b==0

  lab var return_1 "=1 return if not feel better"
  lab var return_2 "=1 return to get a refill of medicine"
  lab var return_3 "=1 return after completition of test"
  lab var return_4 "=1 return after days/weeks"

***********************************************
* SP Doctor's analysis
***********************************************
/*qq6_1: Did you like this doctor?
  qq6_2: Would you go to this doctor again?
  qq6_3: Any other questions asked that were not on the previous list*/

  forvalues n=1/3 {
  recode qq6_`n' (2=0)
    }

  gen     like_doctor=qq6_1
  lab var like_doctor "=1 if SP liked the doctor"

  gen     again_doctor=qq6_2
  lab var again_doctor "=1 if patient back to doctor"

  gen     ask_other=qq6_3
  lab var ask_other "=1 if providers asked other questions"

***********************************************
* GLOBAL ASSESSMENT SCALE
***********************************************
  egen    gas = rmean(g1-g5)
  lab var gas "global assesment scale - max. 15"


***********************************************
* ITR
***********************************************
/*Selected items
  For Diarrhea case
  1: h1a   age of child
  2: h2a   qualities of stool
  3: h3a   frequency
  4: h4a   quantity of stool
  5: h5a   urination
  6: h6a   active/playful
  7: h7a   fever
  8: h8a   abdominal pain
  9: h9a   vomiting
  10: h13a what has the child eaten
  11: h14a taking fluids
  12: t3   Counseling on hygiene specially washing hands (diarrhea) */

* Selected items should begin with the same string
  local i 1
  foreach v of varlist h1a-h9a h13a h14a t3 {
    gen     sp_item`i' = `v'
    lab var sp_item`i' "IRT: Item `i'"
    local i= `i' +1
      }
  order sp_item*, seq last

* Estimate theta for providers who treated cases of diarrhea
  preserve
  keep if sample==1
  keep prov_id sp_item1-sp_item12
  openirt, id(prov_id) item_prefix(sp_item) save_item_parameters("$irtdata1\sp_items_parameters_1.dta") save_trait_parameters("$irtdata1\sp_traits_1.dta")
  restore

* Merge data with Theta Score
  gen id=prov_id
  merge 1:1 id using "$irtdata1\sp_traits_1.dta"
  renpfix theta sp_theta
  drop _merge
  drop id

* Standardizing thetas
  egen    sp_theta_eap_std = std(sp_theta_eap)
  lab var sp_theta_eap_std "Standardized Theta (EAP)"
  egen    sp_theta_mle_std = std(sp_theta_mle)
  lab var sp_theta_mle_std "Standardized Theta (MLE)"
  drop sp_theta_pv1-sp_theta_pv5 sp_theta_mle_se

***********************************************
* Know-do Gap
* 12 Main questions
***********************************************

  local i 1
  forvalues n=2/9 {
    gen  sp_dq`i' = h`n'a
    local i = `i' +1
    }

  gen sp_dq9  = h11a
  gen sp_dq10 = h13a
  gen sp_dq11 = h14a
  gen sp_dq12 = h16a

  lab var sp_dq1 "Diarrhea: Qualities of stool"
  lab var sp_dq2 "Diarrhea: Frequency"
  lab var sp_dq3 "Diarrhea: Quantity of stool"
  lab var sp_dq4 "Diarrhea: Questions about urination"
  lab var sp_dq5 "Diarrhea: Is the child active/playful?"
  lab var sp_dq6 "Diarrhea: Fever"
  lab var sp_dq7 "Diarrhea: Abdominal pain"
  lab var sp_dq8 "Diarrhea: Vomiting"
  lab var sp_dq9 "Diarrhea: Source of drinking water"
  lab var sp_dq10 "Diarrhea: What has the child eaten?"
  lab var sp_dq11 "Diarrhea: Taking fluids"
  lab var sp_dq12 "Diarrhea: Physical Environment"

* IRT Performance based on 12 questions

* Estimate theta for providers who treated cases of diarrhea
  preserve
  keep if sample==1
  keep prov_id sp_dq1-sp_dq12
  openirt, id(prov_id) item_prefix(sp_dq) save_item_parameters("$irtdata1\sp_items_parameters_1_q12.dta") save_trait_parameters("$irtdata1\sp_traits_1_q12.dta")
  restore

* Merge data with Theta Score based on 12 questions
  gen id=prov_id
  merge 1:1 id using "$irtdata1\sp_traits_1_q12.dta", keepus(theta_eap theta_mle) 
  rename theta_eap sp_theta_eap_q12
  rename theta_mle sp_theta_mle_q12
  drop _merge
  drop id

* Standardizing thetas
  egen    sp_theta_eap_q12_std = std(sp_theta_eap_q12)
  lab var sp_theta_eap_q12_std "Standardized Theta (EAP)"
  egen    sp_theta_mle_q12_std = std(sp_theta_mle_q12)
  lab var sp_theta_mle_q12_std "Standardized Theta (MLE)"

***********************************************
* Saving processed data
***********************************************
  gen last=.
  order prov_id, after(form_num)
  keep sample2-cluster case-last
  drop last
  order source, first
  sort prov_id
  label data "Standardized Patient Diarrhea Baseline"
  compress
  save "$prodata1\standardized_patient_diarrhea_b", replace











*====================================================================*
* PNEUMONIA
*====================================================================*

* Open database
  use "$prodata1\standardized_patient_pneumonia", clear

* Sample Selection II
/*exclude doctor that only asked the father to bring the child
  c1 did doctor ask to bring the child
  c2 treatment not given(1)/given(2) */
  egen qmiss=rowmean(h1a-h16b)
  replace sample3 = 3 if c1==1 & c2==1 & qmiss==2
  replace sample3 = 4 if c1==2 & c2==1 & qmiss==2
  replace sample3 = 5 if prov_id==16102 // not interviewed
  lab def sample3 1 "complete" 2 "incorrect statement" 3 "child + nothing" 4 "neither child nor treatment" 5 "not interviewed"
  lab val sample3 sample3
  drop qmiss

***********************************************
* General Questions
***********************************************

* Source
  gen     source = "SP Baseline"
  lab var source "Data source"

* Wave
  gen     wave = 1
  lab var wave "Wave"

* Date SP interaction
  replace qq10="25.09.2011" if qq10=="25.29.2011"
  gen     sp_date=date(qq10,"DMY")
  format  sp_date %d
  lab var sp_date "Date of SP-Provider Interaction"

* SP id
  gen     spp_id = qq11a 
  lab var spp_id "Standarized Patient id"
  
* Interviewer id
  gen     intv_id = qq12a
  lab var intv_id "Interviewer id"

* Day of week for visit
  gen day =day(sp_date)
  gen month=month(sp_date)
  gen year=year(sp_date)
  gen dow = dow(mdy(month, day,year))
  lab var dow "Day of week of SP interaction"
  lab def week 0 "Sunday" 1 "Monday" 2 "Tuesday" 3 "Wednesday" 4 "Thursday" 5 "Friday" 6 "Saturday"
  lab val dow week
  drop day month year

* Hour of visit
  gen qq8n=qq8
  replace qq8n ="14:46" if qq8=="02:46"
  replace qq8n ="15:54" if qq8=="03:54"
  gen double ctime =clock(qq8n, "hm")
  format ctime %tcHH:MM
  gen hour=hh(ctime)
  gen minute = mm(ctime)
  lab var ctime  "Consultation Time"
  lab var hour   "Hour of SP interaction"
  lab var minute "Minutes of SP interaction"
  drop qq8n

* Visit Length (minutes)
  split qq9, p(:)
  destring qq91 qq92 qq93, replace
  gen     visit_time = qq91*60 + qq92 + qq93/60
  format  visit_time %9.2f
  lab var visit_time "visit length (minutes)"
  gen double vtime =clock(qq9, "hms")
  format vtime %tcMM:SS
  cap drop qq9* 

* Patient Caseload
/*n1: How many patients were waiting when you reached the clinic?
  n2: How many patients were in the clinic when you left? */
  gen     pat_before=n1
  lab var pat_before "No. of patient before visit"
  gen     pat_after=n2
  lab var pat_after "No. of patient after visit"
  egen    caseload=rsum(pat_before pat_after)
  lab var caseload "patient caseload"

* Doctor ask to bring the child
/*c1: Did doctor ask to bring the child?*/
  recode c1 (2=0), gen (ask_child)
  lab var ask_child "=1 if doctor asked to bring the child"

***********************************************
* Medical History
***********************************************
/*For Pneumonia case (h?a)
  H1: Age of child
  H2: Fever
  H3: Breathing is rapid
  H4: Difficulty in breathing/nostrils flaring/skin between ribs 
      moves inwards/neck muscles are strained
  H5: Type of cough: Cough is continuous/Cough is dry or not
  H6: Runny nose or nose blocked
  H7: Sounds while breathing
  H8: Change in color on fingernails, hands,toes
  H9: Questions about urination/ tears
  H10: Child is weak
  H11: Breastfeeding/ Immunization history
  H12: History of Asthma/ Allergies
  H13: Ear ache */

* Recoding variables
  foreach v of varlist h1a-h16b {
    replace `v'=0 if `v'==2
      }

* Recoding values according to extra questions
  replace h3a=1 if prov_id==35001
  replace h4a=1 if prov_id==10505
  replace h4a=1 if prov_id==31004
  replace h4a=1 if prov_id==31301
  replace h4a=1 if prov_id==35005
  replace h9a=1 if prov_id==20702
  replace h12a=1 if prov_id==10604
  replace h12a=1 if prov_id==26602
  replace h12a=1 if prov_id==35005
  replace h12a=1 if prov_id==44504


* Reorder variables
  order h1b, after(h16a)
  forvalues n=2/16 {
  local nn=`n'-1
  order h`n'b, after(h`nn'b)
    }

* Number of Medical History questions asked
  egen    nhistory = rowtotal(h1a-h14a), missing
  lab var nhistory "No. of Medical History questions asked"

* Essential question for cause of desease
/*h2a fever 
  h3a breathing is rapid
  h4a difficulty in breathing*/
  egen    qcause=rsum(h2a-h4a)
  lab var qcause "No. of ques. asked related to cause of disease"

* Essential question for severity of desease
/*h8a change in colour
  h9a urination/tears
  h10a  child is weak*/
  egen    qseverity = rsum(h8a-h10a)
  lab var qseverity "No. of ques. asked related to severity of disease"

* Number of essential items obtained by providers in history-taking
  egen    nessential=rsum(qcause qseverity)
  lab var nessential "No. of essential items obtained in history-taking"

* Percentage of essential items obtained by providers in history-taking
  gen     pessential = nessential
  replace pessential = pessential/6
  lab var pessential "% of essential items obtained in history-taking"

***********************************************
* Social History
***********************************************
/*For Pneumonia case
  H14: Father smoking
  H15: Chula
  H16: Other children have similar fever/cough*/

* Social history taking
  egen    qsocial=rsum(h14a h15a h16a)
  replace qsocial=1 if qsocial>1 & qsocial!=.
  lab var qsocial "=1 if provider asked about social history"

***********************************************
* Treatment
***********************************************

* Any Treatment
/*For Diarrhea case it seem that this variable takes values 2 (yes)
  whenever medicines is prescribed/dispensed but the same didn't 
  apply for the some pneumonia cases */
  recode c2 (1=0) (2=1), gen(tx_any)
  egen aux=rownonmiss(t1_?med t2_?med), strok
  replace tx_any=1 if (aux>0 & aux!=.)
  lab var tx_any "=1 if provider gave treatment"
  drop aux

* Recode Variables
  forvalues n=1/3 {
    recode t`n' (2=0)
      }

* Medicines dispensed/prescribed
/*t1: Medicines dispensed; t2: Medicines prescribed*/
  gen     med_dispensed = t1
  lab var med_dispensed "medicines dispensed"
  gen     med_prescribed = t2
  lab var med_prescribed "medicines prescribed"

***********************************************
* Correct Treatment
***********************************************

* Merge
  sort case prov_id
  merge 1:1 case prov_id using "$rawdatap1\correct_treatment_sp.dta"
  keep if _merge==3
  drop _merge tx_detail1 nantibiotics_c1

* Rename
  lab var tx_detail3 "detailed treatment"
  lab var nmedicines "# of medicines prescribed"
  rename nantibiotics_c3 nantibiotics3
  lab var nantibiotics3 "# of antibiotics"

* Counselling on hygiene especially washing hands
  gen     counselling = t3
  lab var counselling "=1 if counselling on hygiene"

* Explanation about the medicines.
  gen     explanation = t4
  lab var explanation "=1 if provides explanation about medicines"
  lab def exp 1 "Self-explained" 2 "SP asked" 3 "No one"
  lab val explanation exp

* Explanation given by whom?
  gen     expl_who = t5
  lab var expl_who "who gave the explanation"
  lab def expl 1 "doctor" 2 "Compounder" 3 "nurse"
  lab val expl_who expl 

* Number of questions if treatment/no treatment
  gen     qs_treat  = nhistory if tx_any==1
  lab var qs_treat "number of questions asked when treatment given"

  gen     qs_notreat = nhistory if tx_any==0
  lab var qs_notreat "number of questions asked when no treatment given"

***********************************************
* Diagnosis
***********************************************

* Recode
  foreach v of varlist d1 d3 {
  recode `v' (2=0)
    }

* Any Diagnosis
  gen     dx_any = d1
  lab var dx_any "=1 if any kind of diagnosis was given"

* Diagnosis
  gen     dx = d2
  lab var dx "Diagnosis"

* Type of dignosis given
  gen code=soundex(d2)
  gen     dx_type = 0
  replace dx_type = 1 if (code=="M231" | code=="P555" | code=="S513")
  lab var dx_type "=1 if diagnosis was corrrect, when done"
  drop code

***********************************************
* Fees
***********************************************

* Fee
  gen     fee = f1
  lab var fee "fee charged by practitioner"

  gen     fee_med =f2
  lab var fee_med "cost of medicines paid in addition"

  gen     fee_total=f3
  lab var fee_total "total cost"

***********************************************
* Follow up questions
***********************************************
/*R1:Did the doctor ask you to return? if yes ,then mark the following options If no=2 go to next section
  R2:If you do not feel better. 
  R3: To get a refill of the medicine 
  R4: After completion of recommended test 
  R5a: After so many days
  r5b: Weeks */

  gen     ask_return=(r1==1)
  lab var ask_return "=1 if doctor ask the patient to return"

* When did he/she asked you to return
  forvalues n=1/3 {
  local m=`n'+1
  gen return_`n'=(r`m'==1) if ask_return==1
    }
  gen     return_4 =(r5a!=.|r5b!=.) if ask_return==1
  replace return_4 = 0 if r5a==0 & r5b==0

  lab var return_1 "=1 return if not feel better"
  lab var return_2 "=1 return to get a refill of medicine"
  lab var return_3 "=1 return after completition of test"
  lab var return_4 "=1 return after days/weeks"

***********************************************
* SSP Doctor's analysis
***********************************************

  forvalues n=1/3 {
  recode qq6_`n' (2=0)
    }

  gen     like_doctor=qq6_1
  lab var like_doctor "=1 if SP liked the doctor"

  gen     again_doctor=qq6_2
  lab var again_doctor "=1 if patient back to doctor"

  gen     ask_other=qq6_3
  lab var ask_other "=1 if providers asked other questions"

***********************************************
* GLOBAL ASSESSMENT SCALE
***********************************************
  egen    gas = rmean(g1-g5)
  lab var gas "global assesment scale - max. 15"


***********************************************
* ITR
***********************************************
/*Selected items
  For Pneumonia case
  13: h1a  age of child
  14: h2a  fever 
  15: h3a  breathing is rapid
  16: h4a  difficulty in breathing
  17: h5a  type of cough
  18: h6a  runny nose or nose blocked
  19: h7a  sounds while breathing
  20: h10a child is weak
  21: h11a breastfeeding/immunization history
  22: h16a othr children have similar fever/cough*/

* Selected items should begin with the same string
  local i 1
  foreach v of varlist h1a-h7a h10a h11a h16a {
  gen sp_item`i' = `v'
  lab var sp_item`i' "IRT: Item `i'"
  local i= `i' +1
    }
  order sp_item*, seq last

* Estimate theta for providers who treated cases of pneumonia
  preserve
  keep if sample==1
  keep prov_id sp_item1-sp_item10
  openirt, id(prov_id) item_prefix(sp_item) save_item_parameters("$irtdata1\sp_items_parameters_2.dta") save_trait_parameters("$irtdata1\sp_traits_2.dta")
  restore

* Merge data with Theta Score
  gen id=prov_id
  merge 1:1 id using "$irtdata1\sp_traits_2.dta"
  renpfix theta sp_theta
  drop _merge
  drop id

* Standardizing thetas
  egen    sp_theta_eap_std = std(sp_theta_eap)
  lab var sp_theta_eap_std "Standardized Theta (EAP)"
  egen    sp_theta_mle_std = std(sp_theta_mle)
  lab var sp_theta_mle_std "Standardized Theta (MLE)"
  
  drop sp_theta_pv1-sp_theta_pv5 sp_theta_mle_se
  

***********************************************
* Know-do Gap
* 12 Main questions
***********************************************

* Pneumonia
  local i 1
  forvalues n=2/8 {
    gen sp_pq`i' = h`n'a
    local i = `i' +1
    }

  gen sp_pq8 = h10a
  gen sp_pq9 = h11a
  gen sp_pq10 = h12a
  gen sp_pq11 = h15a
  gen sp_pq12 = h16a

  lab var sp_pq1 "Pneumonia: Fever"
  lab var sp_pq2 "Pneumonia: Breathing is rapid"
  lab var sp_pq3 "Pneumonia: Difficulty in breathing/nostrils flaring/skin between ribs moves inwards/neck muscles are strained"
  lab var sp_pq4 "Pneumonia: Type of cough"
  lab var sp_pq5 "Pneumonia: Runny nose or nose blocked"
  lab var sp_pq6 "Pneumonia: Sounds while breathing"
  lab var sp_pq7 "Pneumonia: Change in colour of fingernails, hand, toes"
  lab var sp_pq8 "Pneumonia: Child is weak"
  lab var sp_pq9 "Pneumonia: Breastfeeding/immunization history"
  lab var sp_pq10 "Pneumonia: History of asthma/alleries"
  lab var sp_pq11 "Pneumonia: Have a chula at home"
  lab var sp_pq12 "Pneumonia: Other children have similar fever/cough"

* IRT Performance based on 12 questions

* Estimate theta for providers who treated cases of pneumonia
  preserve
  keep if sample==1
  keep prov_id sp_pq1-sp_pq12
  openirt, id(prov_id) item_prefix(sp_pq) save_item_parameters("$irtdata1\sp_items_parameters_2_q12.dta") save_trait_parameters("$irtdata1\sp_traits_2_q12.dta")
  restore

* Merge data with Theta Score based on 12 questions
  gen id=prov_id
  merge 1:1 id using "$irtdata1\sp_traits_2_q12.dta", keepus(theta_eap theta_mle) 
  rename theta_eap sp_theta_eap_q12
  rename theta_mle sp_theta_mle_q12
  drop _merge
  drop id

* Standardizing thetas
  egen    sp_theta_eap_q12_std = std(sp_theta_eap_q12)
  lab var sp_theta_eap_q12_std "Standardized Theta (EAP)"
  egen    sp_theta_mle_q12_std = std(sp_theta_mle_q12)
  lab var sp_theta_mle_q12_std "Standardized Theta (MLE)"


***********************************************
* Saving processed data
***********************************************
  gen last=.
  order prov_id, after(form_num)
  keep sample3-cluster case-last
  drop last
  order source, first
  sort prov_id
  label data "Standardized Patient Pneumonia Baseline"
  compress
  save "$prodata1\standardized_patient_pneumonia_b", replace
  log close
